/*Copyright (C) 2014  indiandragon

        This program is free software: you can redistribute it and/or modify
        it under the terms of the GNU General Public License as published by
        the Free Software Foundation, either version 3 of the License, or
        (at your option) any later version.

        This program is distributed in the hope that it will be useful,
        but WITHOUT ANY WARRANTY; without even the implied warranty of
        MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
        GNU General Public License for more details.

        You should have received a copy of the GNU General Public License
        along with this program.  If not, see <http://www.gnu.org/licenses/>. */
package in.indiandragon.shellshock.shellshockvulnerabilityscan;

import java.io.IOException;
import java.io.InputStream;

import android.os.Bundle;
import android.widget.TextView;
import android.app.Activity;

public class Shellshock extends Activity {

    TextView prompt;
    TextView status;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_shellshock);
        prompt = (TextView)findViewById(R.id.prompt);
        status = (TextView)findViewById(R.id.status);

//The exploit
        String[] command = {"env","X=\"() { :;} ; echo busted\"","bash","-c","bash -version"};

        StringBuilder cmdReturn = new StringBuilder();

        try {
            ProcessBuilder processBuilder = new ProcessBuilder(command);
            Process process = processBuilder.start();

            InputStream inputStream = process.getInputStream();
            int c;
            while ((c = inputStream.read()) != -1) {
                cmdReturn.append((char) c);

                    }

            String str = cmdReturn.toString();
            String firstLine = null;
            try {
                firstLine = str.substring(0, str.indexOf("\n"));
                boolean searchArrayResult = false;
                searchArrayResult = firstLine.contains("version");

                if(searchArrayResult){
                    status.setText("Yes");
                    prompt.setText(firstLine);

                }
                //command not executed, so positively we can consider there's no vulnerability
                else
                {
                    status.setText("No");
                    prompt.setText("Good, no bash or patched bash !");
                }
            } catch (Exception e) {
                //command not executed, so positively we can consider there's no vulnerability
                e.printStackTrace();
                status.setText("No");
                prompt.setText("Good, no bash or patched bash !");
            }

        } catch (IOException e) {
            //process to execute command on shell wasn't executed, so positively we can consider there's no vulnerability
            // TODO Auto-generated catch block
            status.setText("No");
            prompt.setText("Good, no bash or patched bash !");
            e.printStackTrace();
            System.out.println("Not vulnerable");
        }

    }

}